<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>深入理解JavaScript系列（4）：立即调用的函数表达式</title>
    <script type="text/javascript">
        /*
         * 代码片段1
         */
        function makeCounter() {
            var i = 0;
            return function () {
                console.log(++i);
            };

            function alertMsg() {
                console.log(" this is my function ... ");
            }
        }

        var counter1 = makeCounter();
        counter1();
        counter1();

        var counter2 = makeCounter();
        counter2();
        counter2();

        //alert(i);     //无法访问，函数内部变量

        /*
         * 代码片段2
         * DBM0001
         */
        var DBM0001 = {
            PrintNameFunc: function () {
                console.log("this is my first class ... ");
            },
            PrintRealFunc: function () {
                console.log("this is my Real Name ... ");
            },
            ReturnFirstName: function () {
                return "梦炜";
            }
        };

        //调用 【类中的方法】
        DBM0001.PrintNameFunc();
        DBM0001.PrintRealFunc();
        console.log(DBM0001.ReturnFirstName());


        /*
         * 自执行
         */
        var foo = function () {
            console.log("自执行函数11。。。");
        }
        foo();

        (function () {
            console.log("自执行函数22。。。");
        }());

        /*
         * 创建一个立即调用的匿名函数表达式
         * return一个变量，其中这个变量里包含你要暴露的东西
         */
        var nonamefunc = (function () {
            var i = 0;
            return{
                get: function () {
                    console.log("get 函数。。。");
                    return i;
                },
                set: function (val) {
                    console.log("set 函数。。。");
                    i = val;
                },
                increment: function () {
                    console.log("自增函数。。。");
                    return ++i;
                }
            };
        }());

        nonamefunc.get();
        nonamefunc.set(4);
        console.log(nonamefunc.increment());

        function ADDOne() {
            alert(nonamefunc.increment());
        }
    </script>
</head>
<body>
<input id="input1" type="button" value="自增" onclick="ADDOne()">
</body>
</html>